package com.hxkj.client.service.taglib.directive;

import com.hxkj.client.service.TagsServiceClient;
import com.hxkj.domain.context.EnvironmentContextHolder;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.Tags;
import com.hxkj.domain.po.WebsiteAddr;
import com.hxkj.utils.TemplateModelUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName TagDirective tag信息标签
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/8/21 12:04
 * @Version 1.0
 */
@Component
@Log4j
public class TagDirective extends AbstractDirective {
    @Autowired
    private TagsServiceClient tagsServiceClient;

    /**
     * 输入参数，tagsID
     */
    public static final String TAGS_ID = "tagsId";
    /**
     * 输入参数，站点ID
     */
    public static final String SITE_ID = "siteId";

    /**
     * tag内容
     */
    public static final String TAG = "tag";


    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
                        TemplateDirectiveBody body) throws TemplateException, IOException {
        env.getOut();
        try {
            //Mr.zheng:获取参数
            Long tagsId = super.getLong(TAGS_ID, params) == null ? (Long) super.getCustomAttrbute(env, TAGS_ID) : super.getLong(TAGS_ID, params);
            Long siteId = super.getLong(SITE_ID, params) == null ? (Long) super.getCustomAttrbute(env, SITE_ID) : super.getLong(SITE_ID, params);
            String note = super.getNote(env);

            Tags tags = new Tags();
            tags.setTagsId(tagsId);
            tags.setSiteId(siteId);

            tags = tagsServiceClient.getTags(tags);
            if (StringUtils.isNotBlank(note)) {
                WebsiteAddr websiteAddr = getWebSiteAddr(siteId);
                tags.setManuscriptPath(websiteAddr.getIpAdd() + tags.getManuscriptPath());
            }else {
                // 静态文件文章链接加前缀
                Map<String, Object> enviroment = EnvironmentContextHolder.getEnvironment();
                Map<Long, String> siteCtxMap = (Map<Long, String>) enviroment.get("siteCtxMap");
                String urlContext = siteCtxMap.get(siteId);
                if(StringUtils.isNotBlank(urlContext)) {
                    tags.setManuscriptPath(urlContext + tags.getManuscriptPath());
                }
            }
            env.setVariable(TAG, DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(tags));

            String userToken = super.getUserToken(env);
            if (body != null) {
                if (StringUtils.isEmpty(note)) {
                    body.render(env.getOut());
                } else {
                    Map<String, String> maps = new LinkedHashMap<>();
                    try {
                        super.print(body, maps, env, userToken, params);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else {
                throw new RuntimeException("tags所在模板至少要加一个空格");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
